/*
 * @Author: shanghongyun 2879455630@qq.com
 * @Date: 2025-05-28 14:18:13
 * @LastEditors: shanghongyun 2879455630@qq.com
 * @LastEditTime: 2025-06-20 11:15:45
 * @FilePath: \A05\Dev\dev.c
 * @Description: 外设管理文件
 */
#include "dev.h"
#include "systick.h"
#include "task.h"

//转换序列下标
uint8_t gEADC_Convert_Pos = 0;
//ADC转换序列
uint32_t gEADC_convert_Seq[] = {
    ADC0_CUR_R_Channel  | ADC1_CUR_L_Channel    ,
    ADC0_VOUT_L_Channel | ADC1_VOUT_R_Channel   ,
    ADC0_VBAT_L_Channel | ADC1_PCB_TEMP_Channel ,
    ADC0_CIN__Channel   | ADC1_CUR_L_Channel    ,
    ADC0_VBAT_R_Channel | ADC1_CUR_L_Channel 
};


void SYS_Init(void)
{
    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Enable 48MHz HIRC */
    CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

    /* Waiting for 48MHz clock ready */
    CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

    /* HCLK Clock source from HIRC */
    CLK_SetHCLK(CLK_HCLK_SRC_HIRC,CLK_CLKDIV_HCLK(1));

    /* Enable IP clock */
    CLK_EnableModuleClock(EPWM_MODULE);
    CLK_EnableModuleClock(EADC_MODULE);
    CLK_EnableModuleClock(PGA_MODULE);

    /* Update System Core Clock */
    /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and cyclesPerUs automatically. */
    SystemCoreClockUpdate();

    /*开启欠压检测 3.0V 中断*/
    SYS->BODCTL = SYS_BODCTL_BODEN_Msk | (0x3 << 1);

    /* Lock protected registers */
    SYS_LockReg();

    NVIC_EnableIRQ(BOD_IRQn);
    NVIC_SetPriority(BOD_IRQn,0);	//最高优先级
    
}



void Timer1_Init(void)
{
    /* Enable IP clock */
    CLK_EnableModuleClock(TMR1_MODULE);

    /* Select IP clock source */
    CLK_SetModuleClock(TMR1_MODULE, CLK_TMR1_SRC_HIRC, 0);

    TIMER_SET_PRESCALE_VALUE(TIMER1, 47);//48分频，1mhz
    /* Timer1 set to CONTINUOUS shot mode */
    TIMER1-> CTL |=  TIMER_CTL_RSTCNT_Msk | TIMER_CONTINUOUS_MODE | TIMER_CTL_CNTDATEN_Msk;	//数据寄存器更新当定时器计数器有效
    TIMER1-> CTL |=  TIMER_CTL_CNTEN_Msk ;   //启动T1

//	TIMER1->CTL |= TIMER_CTL_INTEN_Msk; 	// 开中断，在BLDCstart启动中断
    
    TIMER1->CMP = 1000;

//    NVIC_EnableIRQ(TMR1_IRQn);
//    NVIC_SetPriority(TMR1_IRQn,1);
}


/**
 * @description: 外设初始化
 * @return {*}
 */
void dev_init(void){
    /*时钟初始化*/
    SYS_Init();

    /*systick初始化*/
    Systick_Init();

    /* GPIO初始化 */
    gpio_config();

    /*PGA初始化*/
    PGA_config();

    /* ADC初始化 */
    eadc_config();

    /* EPWM初始化 */
    epwm_config();
    
//    /*Timer1初始化*/
//    Timer1_Init();

    /* 初始化任务 */
    Task_init();

    /* 初始化RTT */
    SEGGER_RTT_Init();
}


